package com.chezhujie.www.service;

import com.chezhujie.www.domain.SettleAccounts;
import com.chezhujie.www.persistence.SettleAccountsMapper;
import com.sys.core.exception.ClassCheckException;
import com.sys.core.page.Pagination;
import com.sys.core.utils.LocalDateTime;
import com.sys.core.utils.MapManage;
import com.sys.user.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.*;


/**
 * 结算
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class SettleAccountsService extends BaseService<SettleAccounts> {
    @Autowired
    private SettleAccountsMapper settleaccountsMapper;
    @Autowired
    private CzjbusinessService czjbusinessService;

    @Autowired
    public void setBaseMapper(SettleAccountsMapper settleaccountsMapper) {
        this.baseMapper = settleaccountsMapper;
    }


    public void checkEntity(SettleAccounts settleaccounts) throws ClassCheckException {
        if(settleaccounts.getSettleaccountsId()!=null&&settleaccounts.getSettleaccountsId().length()>19){
            throw new ClassCheckException("结算主键不能超过19个字符");
        }
        if(settleaccounts.getOrderNo()!=null&&settleaccounts.getOrderNo().length()>32){
            throw new ClassCheckException("结算订单号不能超过32个字符");
        }
        if(settleaccounts.getSettleType()!=null&&settleaccounts.getSettleType().length()>1){
            throw new ClassCheckException("结算结算类型不能超过1个字符");
        }
        if(settleaccounts.getBusinessId()!=null&&settleaccounts.getBusinessId().length()>19){
            throw new ClassCheckException("结算商家不能超过19个字符");
        }
    }
    public void checkListEntity(List<SettleAccounts> listSettleAccounts) throws ClassCheckException {

        for (SettleAccounts settleaccounts : listSettleAccounts) {
            checkEntity(settleaccounts);
        }

    }

    /**
     * 月统计
     * @param pageNo
     * @param pageSize
     * @param params
     * @param model
     * @return
     * @throws Exception
     */
    public Pagination getByPage(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {
        Map<String, List<Map<String, Object>>> maplist = new HashMap<String, List<Map<String, Object>>>();

        Pagination pagination = czjbusinessService.list(pageNo, pageSize, params, model);
        List<Map<String,Object>> listmap = (List<Map<String,Object>>)pagination.getList();
        Integer year = Integer.parseInt(LocalDateTime.getDate(new Date(), "yyyy"));
        Integer month = Integer.parseInt(LocalDateTime.getDate(new Date(), "MM"));
        List<Map<String, Object>> listmp = new ArrayList<Map<String, Object>>();

        List<Map<String, Object>> listData = (List<Map<String, Object>>) pagination.getList();

        List<String> listbusinessId = MapManage.toGetValueByKey("id", listData);
        List<Map<String,Object>> listSta = settleaccountsMapper.queryByInBusinessId(listbusinessId);

        //获取商家年月季度
        Map<String, Map<String, Object>> keylist = MapManage.toGetKeyMap("businessId",listSta);

        String businessId;
        String yearmonth = "";


        for (int i1 = 0; i1 < listData.size(); i1++) {
            businessId = String.valueOf(listData.get(i1).get("businessId"));
            List<Map<String,Object>> listMa = (List<Map<String,Object>>)listData.get(i1).get("monthlyAccount");
            if(listMa==null){
                listMa = new ArrayList<Map<String, Object>>();
            }

            listMa.add(keylist.get(businessId));
            listData.get(i1).put("monthlyAccount",listMa);//加入月账单
        }












        pagination.setList(listmap);
        return pagination;

    }




}
